package com.colorful.insuranceproject.apush;

import android.content.Context;

import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.colorful.insuranceproject.bean.PushMessage;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;


/**
 * Created by handsome-Bob on 2017/11/10.
 * <p>
 * 接收推送的基础类
 */
public class MyMessageReceiver extends MessageReceiver {

    private Context mContext;

    private static final Object lock = new Object();

    private static MyMessageReceiver mInstance;

    /**
     * 构建一个监听器的接口列表  用来管理和实现推送的方法
     */
    private static final List<APushListener> listeners = new ArrayList<>();

    private static final Comparator<APushListener> comparator = new Comparator<APushListener>() {
        @Override
        public int compare(APushListener lhs, APushListener rhs) {// 2,5
            return rhs.getPriority() - lhs.getPriority();
        }
    };

    /**
     * 获取推送管理实例
     */
    public static MyMessageReceiver getInstance() {
        if (mInstance == null) {
            synchronized (lock) {
                if (mInstance == null) {
                    synchronized (lock) {
                        mInstance = new MyMessageReceiver();
                    }
                }
            }
        }
        return mInstance;
    }

    public void init(Context context) {
        if (mContext == null) {
            this.mContext = context;
        }
    }

    @Override //通知
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        synchronized (lock) {
            Collections.sort(listeners, comparator);
            final int len = listeners.size();
            for (int i = 0; i < len; i++) {
                MyMessageReceiver.APushListener listener = listeners.get(i);
                if (listener != null) {
                    if (!listener.onNotificationPush(context, title, summary, extraMap)) {
                        break;
                    }
                }
            }
        }
    }

    @Override  //消息推送
    public void onMessage(Context context, CPushMessage cPushMessage) {
        synchronized (lock) {
            Collections.sort(listeners, comparator);
            final int len = listeners.size();
            for (int i = 0; i < len; i++) {
                MyMessageReceiver.APushListener listener = listeners.get(i);
                if (listener != null) {
                    if (!listener.onMessagePush(context, cPushMessage)) {
                        break;
                    }
                }
            }
        }
    }

    @Override // 通知被点击后
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
        synchronized (lock) {
            Collections.sort(listeners, comparator);
            final int len = listeners.size();
            for (int i = 0; i < len; i++) {
                MyMessageReceiver.APushListener listener = listeners.get(i);
                if (listener != null) {
                    if (!listener.onNotificationOpenedPush(context, title, summary, extraMap)) {
                        break;
                    }
                }
            }
        }
    }

    @Override // 通知被点击后不做操作
    protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
        synchronized (lock) {
            Collections.sort(listeners, comparator);
            final int len = listeners.size();
            for (int i = 0; i < len; i++) {
                MyMessageReceiver.APushListener listener = listeners.get(i);
                if (listener != null) {
                    if (!listener.onNotificationClickedWithNoActionPush(context, title, summary, extraMap)) {
                        break;
                    }
                }
            }
        }
    }

    @Override // 在应用程序中收到通知
    protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
    }

    @Override // 在通知删除
    protected void onNotificationRemoved(Context context, String messageId) {
    }

    /**
     * 注册一个消息监听器
     *
     * @param listener                         监听器
     * @param onRegisterCallbackRegistrationId 如果registrationId
     */
    public void registerMessageListener(APushListener listener, boolean onRegisterCallbackRegistrationId) {
        synchronized (lock) {
            if (listener != null) {
                listeners.add(listener);
//                if (registrationId != null && onRegisterCallbackRegistrationId) {
//                    listener.onRegistrationId(registrationId);
//                }
            }
        }
    }

    public void unregisterMessageListener(APushListener listener) {
        synchronized (lock) {
            if (listener != null) {
                listeners.remove(listener);
            }
        }
    }

    /**
     * 实现传递的接口类
     */
    public interface APushListener {

        /**
         * 获取优先级
         */
        int getPriority();

        /**
         * 通知
         */
        boolean onNotificationPush(Context context, String title, String summary, Map<String, String> extraMap);

        /**
         * 消息
         */
        boolean onMessagePush(Context context, CPushMessage cPushMessage);

        /**
         * 通知被点击后
         */
        boolean onNotificationOpenedPush(Context context, String title, String summary, String extraMap);

        /**
         * 通知被点击后不做操作
         */
        boolean onNotificationClickedWithNoActionPush(Context context, String title, String summary, String extraMap);
    }
}
